﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using System.Collections;
using AgentBar.Common;
using System.Configuration;

namespace AgentBar
{
    public partial class frmAgentSetting : Form
    {
        private string _AgentIDKey = "AgentID";
        private string _AgentPasswordKey = "AgentPassword";
        private string _AgentExtensionKey = "AgentExtension";
        private string _WaitTimeKey = "WaitTime";
        private string _AgentUseSoftPhoneKey = "AgentUseSoftPhone";

        public frmAgentSetting()
        {
            InitializeComponent();
        }

        private void frmAgentSetting_Load(object sender, EventArgs e)
        {
            try
            {
                GetResource();
                GetAgentSettins();

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void GetResource()
        {
            string language = ConfigurationManager.AppSettings["LAN"];
            if (string.IsNullOrEmpty(language)) language = "JP";
            LanguageResourceManager rm = new LanguageResourceManager(language);
            this.lblAgentID.Text = rm.GetString("strAgentID");
            this.ckbSoftPhone.Text = rm.GetString("strCkbSoftPhone");
            this.lblPassword.Text = rm.GetString("strPassword");
            this.lblSequence.Text = rm.GetString("strSequence");
            this.lblSoftPhone.Text = rm.GetString("strSoftPhone");
            this.lblStandBySeconds.Text = rm.GetString("strStandBySeconds");
            PicBoxStatus picOk = new PicBoxStatus() { Title = "適用", BackGroudImage = Image.FromFile(Application.StartupPath + @"\Image\ButtonOK.png"), PictureBox = this.picOK, Enabled = true };
            PicBoxStatus piccancel = new PicBoxStatus() { Title = "キャンセル", BackGroudImage = Image.FromFile(Application.StartupPath + @"\Image\ButtonCancel.png"), PictureBox = this.picCancel, Enabled = true };
            PictureBoxTool.DrawInPictureBox(picOk);
            PictureBoxTool.DrawInPictureBox(piccancel);
        }

        private void SetAgentSettins()
        {
            Microsoft.Win32.RegistryKey keyName = Microsoft.Win32.Registry.CurrentUser;
            Microsoft.Win32.RegistryKey reg;
            try
            {
                reg = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\NGCPADAPTER", true);
                if (reg == null)
                {
                    reg = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\NGCPADAPTER");
                }
                reg.SetValue(_AgentIDKey, this.txtAgentID.Text.Trim());
                reg.SetValue(_AgentPasswordKey, this.txtPassword.Text.Trim());
                reg.SetValue(_AgentExtensionKey, this.txtSequence.Text.Trim());
                reg.SetValue(_WaitTimeKey, this.txtStandBySeconds.Text.Trim());
                reg.SetValue(_AgentUseSoftPhoneKey, this.ckbSoftPhone.Checked ? "1" : "0");
                reg.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void GetAgentSettins()
        {
            Microsoft.Win32.RegistryKey keyName = Microsoft.Win32.Registry.CurrentUser;
            Microsoft.Win32.RegistryKey reg;
            try
            {
                reg = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\NGCPADAPTER", true);
                if (reg == null)
                {
                    reg = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\NGCPADAPTER");
                }
                this.txtAgentID.Text = reg.GetValue(_AgentIDKey, string.Empty).ToString();
                this.txtPassword.Text = reg.GetValue(_AgentPasswordKey, string.Empty).ToString();
                this.txtSequence.Text = reg.GetValue(_AgentExtensionKey, string.Empty).ToString();
                this.txtStandBySeconds.Text = reg.GetValue(_WaitTimeKey, string.Empty).ToString();
                this.ckbSoftPhone.Checked = reg.GetValue(_AgentUseSoftPhoneKey, "0").ToString() == "1" ? true : false;
                reg.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        private void picCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void picOK_Click(object sender, EventArgs e)
        {
            SetAgentSettins();
            this.Close();
        }


    }
}
